<?php
/**
 * Created by PhpStorm.
 * 楼小二地标变更
 * User: caicm
 * Date: 2017/3/2
 * Time: 9:46
 */

namespace SDXApi\Controller;
use Think\Controller;
use SDXApi\Service\LouxiaoerService;
use SDXApi\Service\OpensearchService;
use Think\Model;

class LouxiaoerEditController extends Controller{

    public function update_landmark(){
        $test = I('test');
        $new_id = I('new_id',0,'int');
        $old_id = I('old_id',0,'int');

        if($test == 'sdx9999' && $new_id && $old_id){

            $where = array(
                'louxiaoer_id' => $old_id
            );

            $data = array(
                'louxiaoer_id' => $new_id
            );

            M('louxiaoer_landmark')->where($where)->data($data)->save();
            M('louxiaoer_order')->where($where)->data($data)->save();
            M('louxiaoer_order_log')->where($where)->data($data)->save();
            M('louxiaoer_user')->where($where)->data($data)->save();

            M('louxiaoer_group')->where($where)->delete();
            M('louxiaoer_group_user')->where($where)->delete();
            print_r('ok');exit;

        }else{
            die('参数有误');
        }
    }

    public function update_dibiao(){
        $test = I('test');
        $new_id = I('new_id',0,'int');
        $old_id = I('old_id',0,'int');
        $region_id = I('region_id',0,'int');

        if($test == 'sdx9999' && $new_id && $old_id && $region_id){
            $where = array(
                'region_id' => $region_id
            );

            $data = array(
                'louxiaoer_id' => $new_id
            );

            M('louxiaoer_landmark')->where($where)->data($data)->save();

            $list = M('louxiaoer_landmark as l')->field('lo.*')->join("left join sdx_louxiaoer_order as lo on l.id=lo.landmark_id")->where('lo.id !=0 and l.region_id='.$region_id)->select();
            foreach($list as $val){
                if($val['id']) M('louxiaoer_order')->where("id=".$val['id'])->data($data)->save();
                if($val['louxiaoer_user_id']) M('louxiaoer_user')->where("id=".$val['louxiaoer_user_id'])->data($data)->save();
                if($val['order_id']) M('louxiaoer_order_log')->where("order_id=".$val['order_id'])->data($data)->save();
            }
            print_r('ok');exit;


        }else{
            die('参数有误');
        }
    }



    //修改商品表评价数量
    public function update_goods_comment(){
        $list = M('Goods g')->field('count(c.goods_id) as num,g.goods_id')->join('left join sdx_comment_goods as c on g.goods_id=c.goods_id')->group('g.goods_id')->having('num > 0')->select();
        foreach($list as $val){
            M('Goods')->where("goods_id=".$val['goods_id'])->data("comment_conut=".$val['num'])->save();

            echo $val['goods_id'].' - '.$val['num'].'<br>';
        }
    }

    /*
     * 绑定地标测试
     */
    public function binding_order(){
        $order_id = I('order_id');

        $binding_louxiaoer_time = gmtime();
        $created = '系统分配';

        $order_list = M('louxiaoer_order')->where("louxiaoer_id=0")->select();
        foreach($order_list as $val){
            $data = array();
            $louxiaoer_order_ = array();
            $louxiaoer = array();
            $order_data = array();
            $test = 0;

            $data = D('LouxiaoerOrder')->findOrderInLouxiaoerLandmark($val['order_id']);

            //根据地址查询绑定楼小二的订单(没有地标的订单以地址匹配)
            $louxiaoer_order_ = D('LouxiaoerOrder')->findLouxiaoerByAddressId($data['address_id']);
            if($louxiaoer_order_){
                $data['id'] = $louxiaoer_order_['id'];
                $data['louxiaoer_id'] = $louxiaoer_order_['louxiaoer_id'];
                $data['name'] = $louxiaoer_order_['name'];
                $data['phone'] = $louxiaoer_order_['phone'];


            //添加楼小二用户
            $louxiaoer = D('LouxiaoerUser')->findLouxiaoerUser($data['louxiaoer_id'], $data['user_id'], $data['address_id']);
            if(!$louxiaoer){
                $louxiaoer_user_id = D('LouxiaoerUser')->addLouxiaoerUser($data['louxiaoer_id'], $data['user_id'], $data['address_id']);
            }else{
                $louxiaoer_user_id = $louxiaoer['id'];
            }

            $order_data = array(
                'louxiaoer_id' => $data['louxiaoer_id'],
                'louxiaoer_user_id' => $louxiaoer_user_id,
                'binding_louxiaoer_time' => $binding_louxiaoer_time,
                'created' => '系统分配',
            );

            $test = M('louxiaoer_order')->where(array('id'=>$val['id']))->data($order_data)->save();
            if($test){
                echo '成功：'.$val['order_id'].'<br>';
            }else{
                echo '失败：'.$val['order_id'].'<br>';
            }
            }
        }
    }

    /*
     * 修改楼小二用户表
     */
    public function update_louxiaoer_user(){
        $data = M('louxiaoer_user as u')->field('u.id,lo.landmark_id')->join("left join sdx_louxiaoer_order as lo on u.id=lo.louxiaoer_user_id")->where('u.landmark_id=0 and lo.id!=""')->select();
        foreach ($data as $val){
            M('louxiaoer_user')->where("id={$val[id]}")->data("landmark_id={$val[landmark_id]}")->save();
            echo $val[id].' - '.$val[landmark_id].'<br>';
        }
    }

    //修改楼小二订单是否首单字段
    public function update_order_first(){
        $data = M('louxiaoer_order')->group('louxiaoer_user_id')->select();
        foreach($data as $val){
            M('louxiaoer_order')->where(array('id'=>$val['id']))->save(array('is_first'=>0));
            echo $val[id].'<br>';
        }
    }

    //绑定楼小二
    public function binding_louxiaoer(){
        $order_id = I('order_id');
        if(!$order_id) die('order_id is null');
        $louxiaoer = new LouxiaoerService();
        $val = $louxiaoer->order_binding_louxiaoer($order_id);
        print_r($val);exit;
    }

    //绑定楼小二脚本
    public function binding_louxiaoer_all(){
        exit;
        $louxiaoer = new LouxiaoerService();
        $add_time = gmtime()-3600;

        $sql = "SELECT o.order_id,o.order_sn   FROM `sdx_order_info` as o LEFT JOIN  `sdx_louxiaoer_order` as lo on o.order_id =lo.order_id  WHERE o.add_time > $add_time and lo.`id` IS NULL and o.`referer`!='891' and o.`pay_status` >1;";

        $data = M()->query($sql);


        foreach ($data as $value){
            $louxiaoer->order_binding_louxiaoer($value['order_id']);
            echo $value['order_sn'].'<br>';
        }

    }

    /*
     * 购物车行为移动
     */
    public function move_cart(){
        set_time_limit(0);
        $page = I('page',1);
        $page = ($page-1)*10000;

        $cart_behavior = M('user_behavior')->field('item_id,user_id,add_time,id')->where("bhv_type='add_cart'")->order('id asc')->limit("$page,10000")->select();
        $data = array();
        foreach ($cart_behavior as $key=>$value){

            $goods_name = '';
            //商品名称
            $goods_name = M('goods')->where(array('goods_id'=>$value['item_id']))->getField('goods_name');


            $data[$key]['source'] = '';
            $data[$key]['version'] = '';
            $data[$key]['goods_id'] = $value['item_id'];
            $data[$key]['uid'] = $value['user_id'];
            $data[$key]['hehavior_type'] = 1;
            $data[$key]['type'] = 1;
            $data[$key]['num'] = 1;
            $data[$key]['surplus_num'] = 1;
            $data[$key]['goods_name'] = $goods_name;
            $data[$key]['add_time'] = $value['add_time']?$value['add_time']:0;



        }
        $val = M('user_behavior_cart')->addAll($data);

    }

    //旧版用户备注迁移到用户标签表
    public function move_user_tag(){
        $remark = M()->query("SELECT u.*,p.`name`  FROM `sdx_louxiaoer_user` as u LEFT JOIN `sdx_warehouse_personnel` as p on u.`louxiaoer_id` =p.`uid`  WHERE u.`remark`  !=''");
        $data = array();
        foreach ($remark as $k=>$v){
            $data[$k]['uid'] = $v['user_id'];
            $data[$k]['order_id'] = 0;
            $data[$k]['address_id'] = $v['address_id'];
            $data[$k]['key_id'] = 11;
            $data[$k]['value'] = $v['remark'];
            $data[$k]['add_time'] = $v['update_time']?$v['update_time']:$v['add_time'];
            $data[$k]['created'] = "【楼小二】{$v['name']}";
        }
        $val = M('user_tag_value')->addAll($data);
        print_r($val);exit;
    }

    /*
     * 搜索关键词处理(做下拉词)
     */
    public function search_keyword_dispose(){

        echo date('Y-m-d H:i:s').'<br>';
        set_time_limit(0);

        $opensearch = new OpensearchService();
        $opensearch->add_search_keyword_pulldown(5);

        echo date('Y-m-d H:i:s').'<br>';

        //获取关键词
        $map['operation_time']  = array('lt',time()-7200);
        $search_keyword = M('search_keyword_pulldown')->where($map)->select();


        foreach ($search_keyword as $value){
            $keyword = $value['keyword'];

            $category_data = array();
            $total = 0;
            $category = '';

            $category_data = A('Opensearch')->get_category_parent($keyword);

            if($category_data){
                $category = serialize($category_data);
                $total = 1;
            }

            $data['category'] = $category;
            $data['total'] = $total;
            $data['operation_time'] = time();

            M('search_keyword_pulldown')->where(array('id'=>$value['id']))->save($data);

        }

        echo date('Y-m-d H:i:s').'<br>';


    }

    /*
     * 关键词处理
     */
    public function keyword_dispose(){
        set_time_limit(0);
        $sql = "SELECT COUNT(*)  as num, keyword FROM ( SELECT * FROM `sdx_search_keyword` WHERE `goods_num` >1 ORDER BY `id` desc) t GROUP BY keyword HAVING num > 0 ORDER BY num asc;";
        $data = M()->query($sql);

        foreach ($data as $value){

            $keyword = '';
            $keyword = trim($value['keyword']);
            //不带中文跳出
            if (!preg_match("/[\x7f-\xff]/", $keyword)) {
                continue;
            }
            $pulldown_id = 0;
            $data['keyword'] = $keyword;
            $data['num'] = $value['num'];
            //关键词是否存在
            $pulldown_id = M('search_keyword_pulldown')->where(array('keyword'=>$keyword))->getField('id');
            if($pulldown_id){
                M('search_keyword_pulldown')->where(array('id'=>$pulldown_id))->save($data);

            }else{
                M('search_keyword_pulldown')->add($data);
            }
        }
    }



}